<?php
#**************************************************************************
#  openSIS is a free student information system for public and non-public 

include('../../Redirect_modules.php');


DrawBC("Attendence > ".ProgramTitle());

Widgets('course');

if(ISSET($_REQUEST['w_course_period_id']))
{
	$_SESSION['w_course_period_id'] = $_REQUEST['w_course_period_id'];
}
else
{
	$_REQUEST['w_course_period_id'] = $_SESSION['w_course_period_id'];
}

$extra['SELECT'] = ",Concat(NULL) AS ABSENCE_TOTAL, NULL AS COURSE_PERIOD_ID ";
$extra['link']['FULL_NAME']['variables'] = array('student_id'=>'STUDENT_ID', 'w_course_period_id'=>'COURSE_PERIOD_ID');
$extra['link']['FULL_NAME']['link'] = "Modules.php?modname=$_REQUEST[next_modname]";
$extra['functions'] = array('ABSENCE_TOTAL'=>'_makeAbsenceTotalValue', 'COURSE_PERIOD_ID'=> '_makeCoursePeriod' );

$extra['columns_after'] = array('ABSENCE_TOTAL'=>'</A>Total absences<A>');
Search('student_id',$extra);
if($_REQUEST['modfunc']=='add' || $_REQUEST['student_id'] || UserStudentID() )
{
    if($_REQUEST['student_id'])
		$RET = DBGet(DBQuery('SELECT FIRST_NAME,LAST_NAME,MIDDLE_NAME,NAME_SUFFIX FROM students WHERE STUDENT_ID=\''.$_REQUEST['student_id'].'\''));
	else
		$RET = DBGet(DBQuery('SELECT FIRST_NAME,LAST_NAME,MIDDLE_NAME,NAME_SUFFIX FROM students WHERE STUDENT_ID=\''.UserStudentID().'\''));
    $count_student_RET=DBGet(DBQuery('SELECT COUNT(*) AS NUM FROM students'));
    if($count_student_RET[1]['NUM']>1){
		DrawHeaderHome( 'Selected Student: '.$RET[1]['FIRST_NAME'].'&nbsp;'.($RET[1]['MIDDLE_NAME']?$RET[1]['MIDDLE_NAME'].' ':'').$RET[1]['LAST_NAME'].'&nbsp;'.$RET[1]['NAME_SUFFIX'].' (<A HREF=Side.php?student_id=new&modcat='.clean_param($_REQUEST['modcat'],PARAM_NOTAGS).'><font color=red>Deselect</font></A>) | <A HREF=Modules.php?modname='.clean_param($_REQUEST['modname'],PARAM_NOTAGS).'&search_modfunc=list&next_modname=Students/Student.php&ajax=true&bottom_back=true&return_session=true target=body>Back to Student List</A>');
    }
	else if($count_student_RET[1]['NUM']==1){
        DrawHeaderHome( 'Selected Student: '.$RET[1]['FIRST_NAME'].'&nbsp;'.($RET[1]['MIDDLE_NAME']?$RET[1]['MIDDLE_NAME'].' ':'').$RET[1]['LAST_NAME'].'&nbsp;'.$RET[1]['NAME_SUFFIX'].' (<A HREF=Side.php?student_id=new&modcat='.clean_param($_REQUEST['modcat'],PARAM_NOTAGS).'><font color=red>Deselect</font></A>) ');
    }
}
/*
if($_REQUEST['modfunc']=='add' && AllowEdit())
{
	
	if($_REQUEST['new_activity']!= '')
	{
		$bExist = DBGet(DBQuery('SELECT * FROM student_fee_activities WHERE SYEAR=\''.UserSyear().'\' AND STUDENT_ID=\''.UserStudentID().'\' AND ACTIVITY_ID=\''.$_REQUEST['new_activity'].'\''));
		if(count($bExist)==0)
		{
			DBQuery('INSERT INTO student_fee_activities (STUDENT_ID,ACTIVITY_ID,SYEAR) values(\''.UserStudentID().'\',\''.$_REQUEST['new_activity'].'\',\''.UserSyear().'\')');
		}
	}
	else 
	{
		$sql1 = 'UPDATE student_fee_activities SET DONE=0 WHERE SYEAR=\''.UserSyear().'\' AND STUDENT_ID=\''.UserStudentID().'\'';
		DBQuery($sql1);
		if(count($_REQUEST['activity_id']))
		{
			$sql2 = 'UPDATE student_fee_activities SET DONE=1 WHERE SYEAR=\''.UserSyear().'\' AND STUDENT_ID=\''.UserStudentID().'\' AND ';
			$list = ' (';		
			foreach($_REQUEST['activity_id'] as $activity_id=>$yes)
			{
				$list.= '  ACTIVITY_ID=\''.$activity_id.'\' OR';
			}
			
			$list = substr($list, 0, -3);
			$list.=') ';
			$sql2.= $list;
			DBQuery($sql2);
		}
	}
	unset($_REQUEST['modfunc']);
}

if($_REQUEST['modfunc']=='remove' && AllowEdit())
{
	if(DeletePrompt('activity'))
	{
		DBQuery('DELETE FROM student_fee_activities WHERE STUDENT_ID=\''.UserStudentID().'\' AND ACTIVITY_ID=\''.$_REQUEST['activity_id'].'\' AND SYEAR=\''.UserSyear().'\'');
		unset($_REQUEST['modfunc']);
	}
}
*/
if(UserStudentID() && !$_REQUEST['modfunc'])
{

	echo '<TABLE border=0 width=90%><TR><TD width=50% valign=top>';
	
	$sqlAbsence = "SELECT DATE_FORMAT(SCHOOL_DATE,'%M/%d/%Y') AS SCHOOL_DATE, sp.TITLE, ATTENDANCE_REASON
		FROM attendance_period ap JOIN school_periods sp ON ap.PERIOD_ID = sp.PERIOD_ID
		WHERE course_period_id = '".$_REQUEST['w_course_period_id']."' 
		AND student_id = '".UserStudentID()."' 
		AND attendance_code
		IN (SELECT id 
			FROM attendance_codes
			WHERE syear = '".UserSyear()."' AND school_id = '".UserSchool()."' AND state_code =  'A'
		)";
	
	$absences_RET = DBGet(DBQuery($sqlAbsence));
	
	//$link['remove']['link'] = "Modules.php?modname=$_REQUEST[modname]&modfunc=remove&start_date=$_REQUEST[start_date]";
//	$link['remove']['link'] = "#"." onclick='check_content(\"ajax.php?modname=$_REQUEST[modname]&modfunc=remove&start_date=$_REQUEST[start_date]\");'";
	//$link['remove']['variables'] = array('activity_id'=>'ACTIVITY_ID');
	//$link['add']['html']['TITLE'] = '<TABLE border=0 cellpadding=0 cellspacing=0><TR><TD>'.SelectInput('','new_activity','',$activities).' </TD></TR></TABLE>';
	//$link['add']['html']['remove'] = button('add');

	echo "<FORM action=Modules.php?modname=$_REQUEST[modname]&modfunc=add&start_date=$_REQUEST[start_date] method=POST>";

	$columns = array('SCHOOL_DATE'=>'Date','TITLE'=>'Period','ATTENDANCE_REASON'=>'Reason');
	
	
	ListOutput($absences_RET,$columns,'Absence','absences',$link);
	echo '<BR><CENTER>'.SubmitButton('Save','','class=btn_medium onclick=\'formload_ajax("elig_stud");\'').'</CENTER>';
	echo '</FORM>';

	echo '</TD>';
	
	echo '</TR></TABLE>';
}

function _makeLower($word)
{
	return ucwords(strtolower($word));
}

function _makeAbsenceTotalValue($value,$title)
{	global $THIS_RET;
	$sqlTotal = "SELECT COUNT( * ) AS TOTAL
		FROM attendance_period
		WHERE course_period_id = '".$_REQUEST['w_course_period_id']."' 
		AND student_id = '".$THIS_RET['STUDENT_ID']."' 
		AND attendance_code
		IN (SELECT id 
			FROM attendance_codes
			WHERE syear = '".UserSyear()."' AND school_id = '".UserSchool()."' AND state_code =  'A'
		)";
	$total = DBGet(DBQuery($sqlTotal));	
	return $total[1]['TOTAL'];
}

function _makeCoursePeriod($value, $title)
{
	global $THIS_RET;
	return $_REQUEST['w_course_period_id'];
}


?>